package com.huixue.zdb.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixue.zdb.pojo.Merchant;
import com.huixue.zdb.service.IMerchantService;
import com.huixue.zdb.util.PageBo;
import com.huixue.zdb.util.ResultBo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 商家表 前端控制器
 * </p>
 *
 * @author ZhouHuRun
 * @since 2020-09-23
 */

@RestController
@RequestMapping("/merchant")
public class MerchantController {

    @Resource
    private IMerchantService merchantService;


    /**
     *  修改或新增数据
     * @return
     */
    @PostMapping("/saveMerchant")
    public ResultBo saveMerchant(@RequestBody Merchant merchant){
        try {
            System.err.println(merchant);
            long time = System.currentTimeMillis();
            //getid等于null是新增
            if(merchant.getMId() == null){ merchant.setCreateTime(time); }
            merchant.setUpdataTime(time);
            System.err.println(merchant);
            this.merchantService.saveOrUpdate(merchant);
            return ResultBo.getInstance().setMsg("保存成功");
        }catch (Exception e){
            return ResultBo.getInstance().setState(500).setMsg("添加失败");
        }
    }


    /**
     * 分页查询商家信息
     * @param pageBo 传入那页 每页大小
     * @return
     */
    @PostMapping("/pageMerchant")
    public ResultBo pageMerchant(@RequestBody PageBo<Merchant> pageBo){
        IPage<Merchant> ipage = this.merchantService.page(new Page<Merchant>(pageBo.getPage(),pageBo.getSize()));
        return ResultBo.getInstance().setMsg("查询成功").setData(ipage);
    }





}

